package com.org.gundutubruk;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ShowPlayer extends Activity{
	
	Context context;
	ImageView panahatas;
	ImageView panahbawah;
	Button panahkiri;
	Button panahkanan;
	TextView namapemain;
	TextView gundupemain;
	TextView menangpemain;
	TextView mainpemain;
	TextView levelpemain;
	ImageView avatarpemain;
	RelativeLayout discroll;
	RelativeLayout hal1;
	RelativeLayout hal2;
	float awal=0;
	float first=0;
	float curry=0;
	boolean isRunning;
	int currPlayer;
	
	int gbrapatar=R.drawable.apatar0;
	Thread tAtas;
	Thread tBawah;
	Thread blink;
	int height;
	int stateBlink;
	public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);    	
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    	setContentView(R.layout.show_player_layout);
    	castItem();
    	init();
    	isiFungsiItem();
    	
	}

	public void onBackPressed(){
		isRunning=false;
		this.startActivity(new Intent("android.intent.action.MENUUTAMA"));
		overridePendingTransition(R.anim.fade, R.anim.hold);
		this.finish();		
	}
	
	private void isiFungsiItem() {
		panahkiri.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(currPlayer>0){
					currPlayer--;
					dispPlayerInfo();
				}
			}
		});
		panahkanan.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(currPlayer<Utility.playerHolder.size()-1){
					currPlayer++;
					dispPlayerInfo();
				}
			}
		});
		panahatas.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				final Handler h = new Handler();
				tAtas = new Thread(){
					public void run(){
		                int y1 = hal1.getScrollY();
		                int y2 = hal2.getScrollY();
		                while(y1>0){
		                    final int Y1 = y1;
		                    final int Y2 = y2;
		                    h.post(new Runnable() {
		                        public void run() {
		                            hal1.scrollTo(0, Y1);
		                            hal2.scrollTo(0, Y2);
		                        }
		                    });
		                    y1-=2;
		                    y2-=2;
		                    panahbawah.setClickable(false);
		                    try {
		                        sleep(100/12);
		                    } catch (InterruptedException e) {
		                    }
		                }
		                panahbawah.setClickable(true);
		            }
				};
				tAtas.start();
			}
		});
		panahbawah.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				final Handler h = new Handler();
				tBawah = new Thread(){
					public void run(){
		                int y1 = hal1.getScrollY();
		                int y2 = hal2.getScrollY();
		                while(y1<480){
		                    final int Y1 = y1;
		                    final int Y2 = y2;
		                    h.post(new Runnable() {
		                        public void run() {
		                            hal1.scrollTo(0, Y1);
		                            hal2.scrollTo(0, Y2);
		                        }
		                    });
		                    y1+=2;
		                    y2+=2;
		                    panahatas.setClickable(false);
		                    try {
		                        sleep(100/12);
		                    } catch (InterruptedException e) {
		                    }
		                }
		                panahatas.setClickable(true);
		            }
				};
				tBawah.start();
			}
		});
	}

	private void init() {
		isRunning=true;
		height=getWindowManager().getDefaultDisplay().getHeight();
		hal2.scrollTo(0, -height);
		if(Utility.playerHolder.size()<=0){
			currPlayer=-1;
			namapemain.setText("---");
			gundupemain.setText("---");
			menangpemain.setText("---");
			mainpemain.setText("---");
			levelpemain.setText("---");
			avatarpemain.setImageResource(R.drawable.apatar);
		}else{
			currPlayer=0;
			dispPlayerInfo();
		}
		stateBlink=1;
		final Handler h = new Handler();
		blink=new Thread(){
			public void run(){
				while(isRunning){
					h.post(new Runnable(){
						public void run(){
							if(stateBlink==1){
								panahatas.setImageResource(R.drawable.atas2);
								panahbawah.setImageResource(R.drawable.bawah2);
								stateBlink=0;
							}else{
								panahatas.setImageResource(R.drawable.atas);
								panahbawah.setImageResource(R.drawable.bawah);
								stateBlink=1;
							}
						}
					});
					try {
						sleep(200);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		};
		blink.start();
	}

	private void dispPlayerInfo() {
		Player P = Utility.playerHolder.get(currPlayer);
		namapemain.setText(""+P.name);
		gundupemain.setText(""+P.jumlahGundu);
		menangpemain.setText(""+P.wincount);
		mainpemain.setText(""+P.gameplayed);
		levelpemain.setText(""+P.unlockLevel);
		avatarpemain.setImageResource(R.drawable.apatar0+P.avatar);
	}

	private void castItem() {
		context=this;
		panahkiri=(Button)findViewById(R.id.panahkiri);
		panahkanan=(Button)findViewById(R.id.panahkanan);
		namapemain=(TextView)findViewById(R.id.namapemain);
		gundupemain=(TextView)findViewById(R.id.gundupemain);
		menangpemain=(TextView)findViewById(R.id.menangpemain);
		mainpemain=(TextView)findViewById(R.id.mainpemain);
		levelpemain=(TextView)findViewById(R.id.levelpemain);
		avatarpemain=(ImageView)findViewById(R.id.avatarpemain);
		discroll=(RelativeLayout)findViewById(R.id.srollable);
		hal1=(RelativeLayout)findViewById(R.id.halsatu);
		hal2=(RelativeLayout)findViewById(R.id.haldua);
		panahatas = (ImageView)findViewById(R.id.panahatas);
		panahbawah=(ImageView)findViewById(R.id.panahbawah);
	}
	
	public boolean onTouchEvent(MotionEvent event){
		final int actioncode = event.getAction() & MotionEvent.ACTION_MASK;
		
			
			if(actioncode==MotionEvent.ACTION_DOWN){
				awal=event.getY();
				first=event.getY();
			}else if(actioncode==MotionEvent.ACTION_MOVE){
				curry=event.getY();
				hal1.scrollBy(0, (int)(awal-curry));
				hal2.scrollBy(0, (int)(awal-curry));
				awal=curry;
			}else if(actioncode==MotionEvent.ACTION_UP){
				curry=event.getY();
				if(first-curry>100){
					hal1.scrollTo(0, height);
					hal2.scrollTo(0, 0);
				}else if(first-curry>0&&first-curry<100){
					hal1.scrollTo(0, 0);
					hal2.scrollTo(0, -height);
				}else if(first-curry<-100){
					hal1.scrollTo(0, 0);
					hal2.scrollTo(0, -height);
				}else if(first-curry<0&&first-curry>-100){
					hal1.scrollTo(0, height);
					hal2.scrollTo(0, 0);
				}
			}
		return false;
	}
	
}
